package com.vframework.system.role.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vframework.base.controller.BaseController;
import com.vframework.common.response.R;
import com.vframework.config.annotation.admin.AdminRestController;
import com.vframework.system.role.entity.SysRole;
import com.vframework.system.role.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-03-27
 */
@AdminRestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;


    @GetMapping
    @PreAuthorize("hasAuthority('sys:role:list')")
    @ApiOperation(value = "角色列表")
    public R<List<SysRole>> list() {
        return R.success(sysRoleService.list(new QueryWrapper<>()));
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:query')")
    @ApiOperation(value = "角色详情")
    public R<SysRole> query(@PathVariable Integer id) {
        SysRole sysRole = sysRoleService.getById(id);
        return R.success(sysRole);
    }

    @PostMapping("add")
    @PreAuthorize("hasAuthority('sys:role:add')")
    @ApiOperation(value = "添加角色")
    public R<SysRole> addRole(@Valid @RequestBody SysRole sysRole, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
            List<String> errorStr = errors.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.toList());
            throw new RuntimeException(errorStr.toString());
        }
        sysRoleService.saveOrUpdate(sysRole, new LambdaUpdateWrapper<SysRole>().eq(SysRole::getId, sysRole.getId()));
        return R.success(sysRole);
    }

    @PostMapping("update")
    @PreAuthorize("hasAuthority('sys:role:update')")
    @ApiOperation(value = "修改角色")
    public R<SysRole> update(@Valid @RequestBody SysRole sysRole, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
            List<String> errorStr = errors.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.toList());
            throw new RuntimeException(errorStr.toString());
        }
        if (BeanUtil.isEmpty(sysRole) || sysRole.getId() == null) {
            throw new RuntimeException("未查询到角色,修改失败");
        }
        sysRoleService.updateById(sysRole);
        return R.success(sysRole);
    }
}
